package com.example.evanproject.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.evanproject.domain.Type;
import com.example.evanproject.service.impl.TypeServiceImpl;
import com.example.evanproject.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@CrossOrigin
@RequestMapping("type")
public class TypeController {
    @Autowired
    private TypeServiceImpl typeService;

    @RequestMapping("selectall")
    public AjaxResult selectall(Integer fid){
        return AjaxResult.success(typeService.selectall(fid)) ;
    }


//
    @RequestMapping("typehasnewlist")
    public AjaxResult text(){
        return AjaxResult.success(typeService.selecthasnews()) ;
    }

//    新闻类别的修改与添加
    @PostMapping("upaddnewtype")
    public  AjaxResult upaddnewtype(@RequestBody Type type){
        type.setTypeFid(0);
        return AjaxResult.success(typeService.saveOrUpdate(type));
    }

//    新闻类别的删除
@RequestMapping("deleteNewType")
public  AjaxResult deleteNewType(Integer id){
    return AjaxResult.success(typeService.removeById(id));
}

    @RequestMapping("selectallbyname")
    public AjaxResult selectallbyname(Integer fid,String name){

        QueryWrapper<Type> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().like(name!=null,Type::getTypeName,name)
                .ge(fid!=null,Type::getTypeFid,fid);
        return AjaxResult.success(typeService.list(queryWrapper)) ;
    }





}
